import React from "react";
import { Navigate, useLocation } from "react-router-dom";

interface AuthRouteProps {
  children: React.ReactNode;
}

const AuthRoute: React.FC<AuthRouteProps> = ({ children }) => {
  const location = useLocation();
  const token = localStorage.getItem("token");

  if (!token) {
    // 将用户重定向到登录页面，但保存他们尝试访问的URL
    return (
      <Navigate
        to="/login"
        state={{ from: location }}
        replace
        data-oid="4bhk9gm"
      />
    );
  }

  return <>{children}</>;
};

export default AuthRoute;
